﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AliboboStockLib.util.io.file;
using HAP = HtmlAgilityPack;
namespace AliboboStockLib.stockupdater.sbdetail
{
    //這個有更好的版本 ,所以廢棄了
    public abstract class OtcDailyUpdater
    {

        public class OtcDailyEntry
        {
            public String 代號;
            public String 名稱;
            public decimal 收盤;
            public decimal 漲跌;
            public decimal 開盤;
            public decimal 最高;
            public decimal 最低;
            public int 成交股數;
            public decimal 成交金額;
            public int 成交筆數;
            public int 漲跌狀態 = 0; // =1 長 =2 漲停  =-1 跌 -2 跌停  
            //最後買價
            //最後賣價
            public long 發行股數;
            public bool isManaged = false;
            //次日參考價 
            //次日漲停價 
            //次日跌停價 
        }
        /***********************************************************************************
         * 
         * 
         * 
         ************************************************************************************/

        
        protected static bool processDecimal(String text, ref decimal value)
        {
            decimal localdec;
            String[] ss=text.Split(' ');
            if (ss.Length == 1)
            {
                if (decimal.TryParse(ss[0], out localdec))
                {
                    value = localdec;
                    return true;
                }
                return false;
            }
            if(ss[0].Contains("+"))
             {
                 if (decimal.TryParse(ss[1], out localdec))
                 {
                     value = localdec;
                     return true;
                 }
                 return false;

             }
            if(ss[0].Contains("-"))
             {
                 if (decimal.TryParse(ss[1], out localdec))
                 {
                     value = 0-localdec;
                     return true;
                 }
                 return false;
             }
            if (decimal.TryParse(ss[1], out localdec))
            {
                value = localdec;
                return true;
            }
            return false;

            

        }
        protected static OtcDailyEntry processRow(HAP.HtmlNode row)
        {
            if (row.ChildNodes.Count != 17)
                return null;

            if (row.ChildNodes[0].InnerText.Contains("代號"))
                return null;

            OtcDailyEntry result = new OtcDailyEntry();

            result.代號 = row.ChildNodes[0].InnerText.Trim();
            result.名稱 = row.ChildNodes[1].InnerText.Trim();

            decimal decvalue = new decimal(0);
            //int intvalue = 0;

            //收盤; 2
            if (processDecimal(row.ChildNodes[2].InnerText.Trim(), ref decvalue))
            {
                result.收盤 = decvalue;
            }
            //漲跌; 3
            if (processDecimal(row.ChildNodes[3].InnerText.Trim(), ref decvalue))
            {
                result.漲跌=decvalue;
            }
            //開盤; 4
            if (processDecimal(row.ChildNodes[4].InnerText.Trim(), ref decvalue))
            {
                result.開盤 = decvalue;
            }
            //最高; 5
            if (processDecimal(row.ChildNodes[5].InnerText.Trim(), ref decvalue))
            {
                result.最高 = decvalue;
            }
            //最低; 6
            if (processDecimal(row.ChildNodes[6].InnerText.Trim(), ref decvalue))
            {
                result.最低 = decvalue;
            }
            //均價 7
            //成交股數; 8
            if (processDecimal(row.ChildNodes[8].InnerText.Trim(), ref decvalue))
            {
                result.成交股數 =Convert.ToInt32(decvalue);
            }
            //成交金額; 9
            if (processDecimal(row.ChildNodes[9].InnerText.Trim(), ref decvalue))
            {
                result.成交金額 = decvalue;
            }
            //成交筆數; 10
            if (processDecimal(row.ChildNodes[10].InnerText.Trim(), ref decvalue))
            {
                result.成交筆數 = Convert.ToInt32(decvalue); 
            }
            //最後買價 11
            //最後賣價 12
            //public int 發行股數; 13
            if (processDecimal(row.ChildNodes[13].InnerText.Trim(), ref decvalue))
            {
                result.發行股數 = Convert.ToInt64(decvalue); 
            }
            return result;
        }

        /*********************************************************************************************************
         * 
         * 
         * ********************************************************************************************************/

        public void processFile(String fileName)
        {
            String content = FileReader.readFile(fileName);
            if (content.Contains("<html>") == false)
                return;

            HAP.HtmlDocument doc = new HAP.HtmlDocument();
            doc.LoadHtml(content);
            HAP.HtmlNodeCollection rows = doc.DocumentNode.SelectNodes("//tr");
            bool isManagered = false;
            List<OtcDailyEntry> datas = new List<OtcDailyEntry>();
            foreach (HAP.HtmlNode row in rows)
            {
                //    HAP.HtmlNode n=row.SelectSingleNode(row.XPath + "/th[@colspan='17']");
                if (row.InnerText.Contains("管理股票"))
                {
                    isManagered = true;
                    continue;
                }
                OtcDailyEntry e = processRow(row);
                if (e == null)
                    continue;
                e.isManaged = isManagered;
                datas.Add(e);
            }

        }
        /*********************************************************************************
         * 
         * 
         * ********************************************************************************/
        protected void UpdateData(List<OtcDailyEntry> datas, String dateStr)
        {
            //更新 code,
            Guid tid=Guid.Empty;
            foreach (OtcDailyEntry e in datas)
            {
                if (StockCodeManager.getStockCode(e.代號, ref tid) == false)
                {
                    StockCodeManager.createOtcStockCode(e.代號, e.名稱, e.發行股數);
                }
            }



        }
    }
}
